package com.zeus.hello;

import com.zeus.util.ActivitiUtil;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/8/31.
 */
public class BranchFlow {
    ProcessEngine processEngine = ActivitiUtil.getProcessEngine();

    @Test
    public void deploymentProcessDefinitionByInputStream(){
        InputStream bpmnIn = this.getClass().getResourceAsStream("/diagrams/branchFlow.bpmn");
        InputStream pngIn  = this.getClass().getResourceAsStream("/diagrams/branchFlow.png");
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .name("分支流程")
                .addInputStream("branchFlow.bpmn", bpmnIn)
                .addInputStream("branchFlow.png", pngIn)
                .deploy();
        System.out.println("deploy ID:"+deployment.getId());
        System.out.println("deploy name:" + deployment.getName());
    }

    @Test
    public void startProcessInstance(){
        //以流程定义对象的key来启动ProcessInstance
        //（1）获取RuntimeService
        String processDefinitionKey = "branchFlow";
        ProcessInstance processInstance = processEngine.getRuntimeService()
                .startProcessInstanceByKey(processDefinitionKey);
        System.out.println("流程实例ID:" + processInstance.getId());
        System.out.println("流程定义ID:" + processInstance.getProcessDefinitionId());

    }

    /**
     * 删除流程定义
     * 删除部署对象就能删除流程定义对象
     */
    @Test
    public void deleteProcessDefinition(){
        //RuntimeService runtimeService = processEngine.getRuntimeService();
        //runtimeService.
        processEngine.getRepositoryService().deleteDeployment("1");
    }

    /**
     * 执行分支的某个流程，
     *
     * 要设置message环境变量
     */
    @Test
    public  void completeMyPersonalTask(){
        String taskId = "7504";
        Map<String,Object> variables = new HashMap<String, Object>();
        variables.put("message","重要");
        processEngine.getTaskService()
                .complete(taskId,variables);
        System.out.println("完成任务：任务ID:"+taskId);
    }
}
